/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */

package org.baatar.opt.genetic.test.x;

import org.baatar.opt.genetic.chromosome.IChromosome;
import org.baatar.opt.genetic.mutation.IMutator;
import org.baatar.util.RandomUtil;

/**
 *
 * @author Ba
 */
public class MutatorMinXSquare implements IMutator<Character, char[]> {
    @Override
    public IChromosome<Character, char[]> mutate(IChromosome<Character, char[]> chromosome,
                        int iteration,
                        float mutationRatio,
                        RandomUtil random) throws CloneNotSupportedException {
        IChromosome<Character, char[]> mutatedChromosome = null;
        for (int i = 0; i < chromosome.getChromosomeLength(); i++) {
            if (random.getRandDouble() <= mutationRatio) {
                if (mutatedChromosome == null)
                    mutatedChromosome = (IChromosome<Character, char[]>) chromosome.clone();
                if (mutatedChromosome.getGeneValue(i).equals('1'))
                    mutatedChromosome.setGeneValue(i, '0');
                else
                    mutatedChromosome.setGeneValue(i, '1');
            }
        }
        return mutatedChromosome;
    }

    /**
     * Clones the this object.
     * 
     * @return Object new cloned object.
     * 
     * @throws CloneNotSupportedException is trown if this object doesnt
     * implements Cloneable interface.
     */
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
